package ru.gerunn.silent.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import ru.gerunn.silent.exception.SilentReadMessageException;
import ru.gerunn.silent.message.Message;
import ru.gerunn.silent.message.TextMessage;

public class ConsoleReader implements Reader {

	private BufferedReader reader;

	public ConsoleReader() {
		this.setReader(new BufferedReader(new InputStreamReader(System.in)));
	}

	public BufferedReader getReader() {
		return reader;
	}

	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}

	public Message read() throws SilentReadMessageException {
		try {
			String string = this.getReader().readLine();
			//return new TextMessage(string == null ? "" : string);
			//return new TextMessage(string);
			return string == null ? null : new TextMessage(string);
		} catch (IOException e) {
			throw new SilentReadMessageException("IOException", e.getCause());
		}
	}

}
